﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Royalsoft.Common;
using System.Data;

namespace NJL.WebManage.SystemAdmin
{
    public partial class AddAdmin : BasePage
    {
        NJL.Model.NJL_SYS_ADMIN model = new Model.NJL_SYS_ADMIN();
        NJL.BLL.NJL_SYS_ADMIN bll = new BLL.NJL_SYS_ADMIN();
        NJL.BLL.NJL_SYS_BASE bllSysBase = new BLL.NJL_SYS_BASE();
        NJL.BLL.NJL_SYS_CS_ADMIN_TYPE bllSysCsAdminType = new BLL.NJL_SYS_CS_ADMIN_TYPE();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlArea.DataSource = bllSysBase.GetList("");
                ddlArea.DataTextField = "WEBSITE_NAME";
                ddlArea.DataValueField = "ID";
                ddlArea.DataBind();

                ddlUserType.DataSource = bllSysCsAdminType.GetList("");
                ddlUserType.DataTextField = "TYPE_NAME";
                ddlUserType.DataValueField = "TYPE_CODE";
                ddlUserType.DataBind();
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            model.CREATE_DATE_TIME = DateTime.Now;
            model.UPDATE_DATE_TIME = DateTime.Now;
            model.ADMIN_NAME = txtUserName.Text.Trim();
            model.ADMIN_PASSWORD = txtPwd.Text.Trim();
            model.ADMIN_PASSWORD_MD5 = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.Trim(), "MD5");
            model.NJL_SYS_BASE_ID = ddlArea.SelectedValue;
            model.TYPE_CODE = ddlUserType.SelectedValue;

            DataSet ds = bll.GetListByName(txtUserName.Text);

            if (ds.Tables[0].Rows.Count > 0)
            {
                Royalsoft.Common.MessageBox.Show(this, "您输入的用户已注册,请重新输入");
                return;
            }
            if (bll.Add(model))
            {
                MessageBox.ShowAndRedirect(this, "添加成功", "AddAdmin.aspx");
            }
        }
    }
}